home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcr / pcr4_4.lha / DIST / boot / CommandLoopCompatibility.c < prev    next >
C/C++ Source or Header  |  1990-05-04  |  5KB  |  204 lines

  1. /* begincopyright
  2.   Copyright (c) 1988 Xerox Corporation. All rights reserved.
  3.   Use and copying of this software and preparation of derivative works based
  4.   upon this software are permitted. Any distribution of this software or
  5.   derivative works must comply with all applicable United States export
  6.   control laws. This software is made available AS IS, and Xerox Corporation
  7.   makes no warranty about the software, its performance or its conformity to
  8.   any specification. Any person obtaining a copy of this software is requested
  9.   to send their name and post office or electronic mail address to
  10.   PCRCoordinator.pa@xerox.com, or to:
  11.     PCR Coordinator
  12.     Xerox PARC
  13.     3333 Coyote Hill Rd.
  14.     Palo Alto, CA 94304
  15.   endcopyright */
  16.  
  17. /*
  18.  * CommandLoopCompatibility.c
  19.  *
  20.  * Simulation of old command interface.  OBSOLESCENT and DISCOURAGED.
  21.  *
  22.  * Demers, April 10, 1990 8:10:54 am PDT
  23.  * 
  24. */
  25.  
  26. #include <xr/ThreadsMsg.h>
  27. #include <xr/CommandLine.h>
  28. #include <xr/CommandLinePrivate.h>
  29. #include <xr/CommandLoop.h>
  30.  
  31.  
  32. static char *XR_currentCommand = NIL;
  33.  
  34. void
  35. XR_SetCurrentCommand(s)
  36.     char *s;
  37. {
  38.     char *buf;
  39.  
  40.     if( s == NIL ) { XR_currentCommand = NIL; return; }
  41.     buf = (char *) XR_malloc( 1+strlen(s) );
  42.     (void) strcpy(buf, s);
  43.     XR_currentCommand = buf;
  44. }
  45.  
  46. char *
  47. XR_GetCurrentCommand()
  48. {
  49.     return XR_currentCommand;
  50. }
  51.  
  52.  
  53. typedef struct XR_CLOldRegistrationRep {
  54.     CommandProc clor_commandProc;
  55.     XR_Pointer clor_clientData;
  56. } * XR_CLOldRegistration;
  57.  
  58. static
  59. XR_CLPROC(XR_CLProc_invokeOldStyleCommandProc)
  60. {
  61.     XR_CLHandleInner clh = ((XR_CLHandleInner)(clce->clce_h));
  62.     XR_CLOldRegistration clor = ((XR_CLOldRegistration)(self->mp_x));
  63.     int len, i;
  64.     char *buf;
  65.     char *pfrom, *pto;
  66.  
  67.     len = 1;
  68.     for( i = 0; i < argc; i++ ) len += (1+strlen(argv[i]));
  69.     buf = (char *) XR_malloc(len);
  70.     if( buf == NIL ) {
  71.         XR_CLErrorMsg "%s: out of memory\n", argv[0]);
  72.         return (-1);
  73.     }
  74.     pto = buf;
  75.     for( i = 0; i < argc; i++ ) {
  76.         pfrom = argv[i];
  77.         while( (*pto++ = *pfrom++) != 0 ) ;
  78.         pto[-1] = ' ';
  79.     }
  80.     pto[0] = 0;
  81.     XR_SetCurrentCommand(buf);
  82.     (**(clor->clor_commandProc))(buf, clor->clor_clientData,
  83.             clor->clor_commandProc);
  84.     return 1;
  85. }
  86.  
  87. void
  88. XR_register(key, proc, doc, clientData)
  89.     char *key;
  90.     CommandProc proc;
  91.     char *doc;
  92.     XR_Pointer clientData;
  93. {
  94.     XR_CLOldRegistration clor;
  95.     XR_MesaProc mp;
  96.     XR_CLRegistration r;
  97.  
  98.     clor = (XR_CLOldRegistration) XR_malloc(sizeof (*clor));
  99.     if( clor == NIL ) XR_Panic("XR_register 0 out of memory");
  100.     clor->clor_commandProc = proc;
  101.     clor->clor_clientData = clientData;
  102.     mp = XR_MakeMesaProc(
  103.             ((XR_UntypedProc)(XR_CLProc_invokeOldStyleCommandProc)),
  104.             ((XR_Pointer)(clor)) );
  105.     if( mp == NIL ) XR_Panic("XR_register 1 out of memory");
  106.     r = XR_CLRegisterProc( XR_globalCLProcsHandle, key, FALSE, doc, mp, TRUE );
  107.     if( r == NIL ) XR_ConsoleMsg("XR_register failure for %s\n", key);
  108. }
  109.  
  110.  
  111. /* UGH: this ought to be more generally available ... */
  112.  
  113. char *
  114. strdup(s)
  115.     char *s;
  116. {
  117.     char *result = NIL;
  118.     if( s != NIL ) {
  119.         result = (char *) XR_malloc(1+strlen(s));
  120.         if( result != NIL ) (void)strcpy(result, s);
  121.     }
  122.     return result;
  123. }
  124.  
  125. /*
  126.  * UGH: this smashes the string IN PLACE!!!
  127.  * I'm leaving it that way, because some client could conceviably
  128.  * depend on that (arrgh), but I'm also returning a COPY of the
  129.  * stripped string to avoid GC problems.
  130.  */
  131.  
  132. char *
  133. XR_strip(s)
  134.     char *s;
  135. {
  136.     char *extrap, *extrap2, *result;
  137.     char *index(), *rindex();
  138.  
  139.     /* start just after that last '/', if there is one. */
  140.     if ((extrap = rindex(s, '/')) == NIL)
  141.         extrap = s-1;
  142.     extrap += 1;
  143.     /* end just before the first '.' after the slash, if there is one. */
  144.     if ((extrap2 = index(extrap, '.')) != NIL)
  145.         *extrap2 = '\0';
  146.     result = (char *) XR_malloc(1+strlen(extrap));
  147.     (void) strcpy(result, extrap);
  148.     if( s ) return(result); /* make s live until after copy! */
  149. }
  150.  
  151.  
  152. #define WORD_BREAK " \t\n\r"
  153.  
  154. static char *
  155. skip(s, span)
  156.     char *s, *span;
  157. {
  158.     return s + strspn(s, span);
  159. }
  160.  
  161. static char *
  162. nskip(s, span)
  163.     char *s, *span;
  164. {
  165.     return s + strcspn(s, span);
  166. }
  167.  
  168.  
  169.  
  170. char *
  171. get_nth_word(s, n)
  172.     char *s;
  173.     int n;
  174. {
  175.     int len;
  176.     char *retval;
  177.  
  178.     s = skip(s, WORD_BREAK);
  179.     for (; n > 0; n--) {
  180.         s = nskip(s, WORD_BREAK);
  181.         s = skip(s, WORD_BREAK);
  182.     }
  183.     len = strcspn(s, WORD_BREAK);
  184.     retval = (char *)calloc(len+1,1);
  185.     strncat(retval, s, len);
  186.     return retval;
  187. }
  188.  
  189. static int _NEVERCALLED()
  190. {
  191.     XR_Panic("_NEVERCALLED was called!");
  192.     strncmp();
  193. }
  194.  
  195.  
  196. XR_Pointer
  197. XR_GetFrameBuffer( index )
  198.     int index;
  199. {
  200.     XR_ConsoleMsg("%? GetFrameBuffer(%d) is OBSOLETE.  Returning NIL.\n",
  201.             index );
  202.     return NIL;
  203. }
  204.